package com.linkme2night.view.search;

import java.util.ArrayList;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.linkme2night.R;
import com.linkme2night.adapters.SearchVenueListAdapter;
import com.linkme2night.controller.FrontController;
import com.linkme2night.controller.SearchController;
import com.linkme2night.model.Venue;
import com.linkme2night.view.CustomMenuActivity;

public class SearchActivity extends CustomMenuActivity {

	private TextView search;
	private ListView listView;
	private SearchController search_controller;
	private Button btn_filter;
	
	//Dialog
	private Dialog dialog;
	private Spinner spn_clubs;
	private Button btn_apply;
	private Button btn_cancel;
	private final String RANKING = "Ranking";
	private final String CLUB_NAME = "Club Name";
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);

		// Set back button on activity header
		getActionBar().setDisplayHomeAsUpEnabled(true);

		listView = (ListView) findViewById(R.id.listVenues);
		search = (TextView) findViewById(R.id.txt_search);
		btn_filter = (Button) findViewById(R.id.btn_filter);
		
		
		search_controller = new SearchController();
		
		
		setupFilter();
		
		setupSearchBar();
		setupList();
	}

	private void setupFilter() {
		btn_filter.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				setupOrderDialogBox();
			}
		});
		
	}

	private void setupOrderDialogBox() {
		dialog  = new Dialog(this);
		dialog.setContentView(R.layout.sort_filter_menu);
		dialog.setTitle("Filter");
		
		spn_clubs = (Spinner) dialog.findViewById(R.id.spinner_clubs);
		spn_clubs = (Spinner) dialog.findViewById(R.id.spinner_clubs);
		btn_apply = (Button) dialog.findViewById(R.id.btn_apply);
		btn_cancel = (Button) dialog.findViewById(R.id.btn_cancel);
		
		setAdapterSpinner();		
		setButtonListenners();
		
		dialog.show();
	}

	
	private void setButtonListenners() {
		btn_cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		
		btn_apply.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(spn_clubs.getSelectedItem().toString().equals(RANKING)){
					setupListOrdered(RANKING);
				}else if(spn_clubs.getSelectedItem().toString().equals(CLUB_NAME)){
					setupListOrdered(CLUB_NAME);
				}
				dialog.dismiss();
			}

		});
		
		
	}

	private void setupListOrdered(String orderby) {
		SearchVenueListAdapter adapter;
		
		if(orderby.equals(RANKING)){
			adapter = new SearchVenueListAdapter(this,
				R.layout.search_venue_listitem, search_controller.getVenuesSortedByRank());
		}
		else {
			adapter = new SearchVenueListAdapter(this,
					R.layout.search_venue_listitem, search_controller.getVenuesSortedByName());
		}
		
		listView.setAdapter(adapter);
	}
	
	
	private void setAdapterSpinner() {
		ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
				R.array.array_sortBy, android.R.layout.simple_spinner_item);
		
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		spn_clubs.setAdapter(adapter);
	}
	

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		if (item.getItemId() == android.R.id.home) {
			finish();
		}
		return super.onMenuItemSelected(featureId, item);
	}

	private void setupList() {

		SearchVenueListAdapter adapter = new SearchVenueListAdapter(this,
				R.layout.search_venue_listitem, search_controller.getVenues());

		listView.setAdapter(adapter);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adap, View view, int index,
					long arg3) {

				Venue temp_venue = (Venue) adap.getAdapter().getItem(index);
				FrontController.openInteractWithVenue(SearchActivity.this,
						temp_venue.getID(), 1);

			}
		});
	}

	private void setupSearchBar() {

		final ArrayList<Venue> array_sort = new ArrayList<Venue>();
		final Venue venues_data[] = search_controller.getVenues();

		search.addTextChangedListener(new TextWatcher() {
			public void afterTextChanged(Editable s) {
				// Abstract Method of TextWatcher Interface.
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// Abstract Method of TextWatcher Interface.
			}

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				int textlength = search.getText().length();
				array_sort.clear();
				for (int i = 0; i < venues_data.length; i++) {
					if (textlength <= venues_data[i].getName().length()) {
						if (search
								.getText()
								.toString()
								.equalsIgnoreCase(
										(String) venues_data[i].getName()
												.subSequence(0, textlength))) {
							array_sort.add(venues_data[i]);
						}
					}
				}
				listView.setAdapter(new SearchVenueListAdapter(
						SearchActivity.this, R.layout.search_venue_listitem,
						array_sort.toArray(new Venue[array_sort.size()])));
			}
		});
	}

	
	class SortDialogBox implements OnClickListener {
		
		
		
		
		
		public SortDialogBox(Context context){
			
			
			
			
					
			
			
			btn_apply.setOnClickListener(this);
			btn_cancel.setOnClickListener(this);
			
			
			dialog.show();
		}


		@Override
		public void onClick(View v) {
			if(v.getId() == btn_cancel.getId()){
				dialog.dismiss();
			}else{
				
			}
			
		}
		
		
	}
}
